package com.ssy.wireless.api.service;

import com.ssy.wireless.entity.ApDevice;
import com.ssy.wireless.entity.ApDeviceTraffic;
import com.ssy.wireless.mapper.ApDeviceMapper;
import com.ssy.wireless.mapper.ApDeviceTrafficMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;

import java.util.Date;

/**
 * Created by cofey on 16/11/22.
 */
@Service
@Transactional(readOnly = true)
public class ApiApDeviceTrafficService {

    @Autowired
    private ApDeviceMapper apDeviceMapper;

    @Autowired
    private ApDeviceTrafficMapper apDeviceTrafficMapper;

    @Transactional
    public void insertTraffic(String apIndex, Double upTraffic, Double downTraffic) {
        Assert.hasLength(apIndex, "AP索引不能为空");
        Assert.notNull(upTraffic, "上行流量不能为空");
        Assert.notNull(downTraffic, "下行流量不能为空");
        ApDevice apDevice = apDeviceMapper.selectByApIndex(apIndex);
        Assert.notNull(apDevice, "当前AP索引[" + apIndex + "]不存在");
        apDeviceMapper.updateTraffic(upTraffic, downTraffic, apDevice.getId(), apDevice.getHotspotId());

        ApDeviceTraffic traffic = new ApDeviceTraffic();
        traffic.setCreateTime(new Date());
        traffic.setApDeviceId(apDevice.getId());
        traffic.setDownTraffic(downTraffic);
        traffic.setUpTraffic(upTraffic);
        traffic.setHotspotId(apDevice.getHotspotId());
        traffic.setEntId(apDevice.getEntId());
        traffic.setApDeviceId(apDevice.getId());
        traffic.setHotspotType(apDevice.getHotspotType());
        traffic.setHotspotId(apDevice.getHotspotId());
        apDeviceTrafficMapper.insert(traffic);
    }
}
